package com.etc.controller.sensor;

import com.etc.entity.sensor.BizSensorConfig;
import com.etc.result.ResultBean;
import com.etc.service.secsor.IBizSensorConfigService;
import com.etc.vo.sensor.BizSensorConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


/**
 * @Author : DDS
 * @Description : 传感器字典表模型 控制层
 * @Date: 2019-11-28 18:20
 */
@Controller
@RequestMapping("bizSensorConfigController")
public class BizSensorConfigController {

    /**
     * 路径常量
     */
    private final String PATH = "statistical/";

    @Autowired
    private IBizSensorConfigService ibizSensorConfigService;

    /**
     * 分页查询
     * @param model
     * @param pageNum 所查询的页数
     * @param bizSensorConfigVo 页面传回的参数
     * @return 统一的返回ResultBean
     */
    @GetMapping("sensor/{pageNum}")
    public String sensor(Model model, @PathVariable("pageNum") Integer pageNum, BizSensorConfigVo bizSensorConfigVo) {
        ResultBean criteria = ibizSensorConfigService.findCriteria(bizSensorConfigVo, pageNum, 20);
        model.addAttribute("list", criteria.getData());
        return PATH + "sensor_list";
    }

    /**
     * 根据id删除
     *
     * @param id 数据id
     * @return 统一的返回ResultBean
     */
    @GetMapping("delete/{id}")
    @ResponseBody
    public ResultBean delete(@PathVariable("id") String id) {
        return ibizSensorConfigService.deleteById(id);
    }

    /**
     * 根据更新
     *
     * @param bizSensorConfig 更改的数据
     * @return 统一的返回ResultBean
     */
    @PostMapping("update")
    @ResponseBody
    public ResultBean update(BizSensorConfig bizSensorConfig) {
        return ibizSensorConfigService.update(bizSensorConfig);
    }

    @PostMapping("upExcel")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "空文件";
        }
        String filename = file.getOriginalFilename();
        ibizSensorConfigService.toExcel(filename,file);
        return "上传成功";
    }

    /**
     * 导成Excel
     *
     * @return 统一的返回ResultBean
     */
    @GetMapping("downExcel")
    public void downExcel(HttpServletResponse response) throws IOException {
        ibizSensorConfigService.downExcel();
        File file = new File("D:\\price.xls");
        FileInputStream fileInputStream = new FileInputStream(file);
        response.setHeader("content-disposition","attachment;fileName=BizSensorConfig.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(fileInputStream,outputStream);
    }

    /**
     * 根据id查询
     * @param id 页面传回的id
     * @param model
     * @return
     */
    @GetMapping("findById/{id}")
    public String findById(@PathVariable("id") String id,Model model) {
        ResultBean bizSensorConfigServiceById = ibizSensorConfigService.findById(id);
        model.addAttribute("sensor", bizSensorConfigServiceById.getData());
        return PATH + "sensor_update";
    }
}
